<template>
  <div class="agenda">
    <Header @stateChange="stateChange" @search="search" ref="header" />

    <el-row class="content">
      <Item v-for="(item, index) in list" :item="item" :key="index" />
      <Mock></Mock>
      <Mock></Mock>
      <Mock></Mock>
      <Mock></Mock>
    </el-row>

    <Pagination
      @currentPageChange="currentPageChange"
      :total="page.total"
      :currentPage="page.currentPage"
      :pageSize="page.pageSize"
    />
  </div>
</template>

<script>
import Mock from "./components/mock.vue";
import Header from "./components/header.vue";
import Pagination from "./components/pagination.vue";
import Item from "./components/item.vue";
import { getAgendaList, getHaveDoneList } from "../../api/agenda/index.js";
export default {
  name: "AgendaView",
  components: { Header, Pagination, Item, Mock },
  data() {
    return {
      list: [],
      page: {
        total: 50,
        currentPage: 1,
        pageSize: 6,
      },
      state: "0", // 该字段也可以通过ref属性拿到
      keyword: "空", // 该字段也可以通过ref属性拿到
    };
  },
  methods: {
    async getList() {
      // 发送ajax请求获取数据
      if (this.state !== "1") {
        console.log(`请求待办事项数据,关键字为：${this.keyword}`);
        const res = await getAgendaList({
          jsonrpc: "2.0",
          method: "FlowEngin.Runtime.GetTodoTasks",
          params: {
            AppId: 0,
            user: "a94220f01dd6-a101-3f43-d897-7db5d75d",
            taskClass: "",
          },
          id: 1,
          tags: {
            userid: "9D9CC6B8-F947-4A8C-B8E4-52CBBD28A31A",
            username: "张三",
            orgid: "a84e2029f9e0-a0d2-ab4e-0522-898af15e",
            orgname: "贵州江习古高速公路TJ3分部",
            companycode: "1000",
            auth_client_id: "test",
            auth_token: "test",
            platformid: "1",
          },
        });
        console.log(res, "查看res/后端返回的数据类型");

        this.list = this.formattionData(res);
        this.page.total = res.total;
      } else {
        console.log(`请求已办事项数据,关键字为：${this.keyword}`);
        // 参数现在写死，后面要改
        const res = await getHaveDoneList({
          // param 是pcwp1.0的数据
          param: {
            jsonrpc: "2.0",
            method: "FlowEngin.Runtime.GetCompleteTasks",
            params: {
              AppId: 0,
              user: "a94220f01dd6-a101-3f43-d897-7db5d75d",
              filter: {
                NameLike: "",
                BillClass: 1,
                StartDate: "2023-01-04T11:31:29.8826701+08:00",
                EndDate: "2023-01-04T11:31:29.8826701+08:00",
              },
            },
            id: 1,
            tags: {
              userid: "9D9CC6B8-F947-4A8C-B8E4-52CBBD28A31A",
              username: "张三",
              orgid: "a84e2029f9e0-a0d2-ab4e-0522-898af15e",
              orgname: "贵州江习古高速公路TJ3分部",
              companycode: "1000",
              auth_client_id: "test",
              auth_token: "test",
              platformid: "1",
            },
          },
          // pages 是pcwp2.0的数据
          page: {
            appId: 0,
            filter: {
              billClass: 0,
              endDate: "",
              firstRecord: 0,
              nameLike: "",
              pageIndex: 1,
              pageSize: 10,
              sortFields: "",
              startDate: "",
            },
            userId: "a94220f01dd6-a101-3f43-d897-7db5d75d",
          },
        });
        console.log(res, "查看res/后端返回的数据类型");
        this.list = this.formattionData(res);
        this.page.total = res.total;
      }
    },
    currentPageChange(currentPage) {
      this.page.currentPage = currentPage;
      console.log(`当前页是：第${this.page.currentPage}页`);
      this.getList();
    },
    stateChange(newState) {
      console.log("最新的状态", newState);
      this.state = newState;
      this.page.currentPage = 1;
      this.getList();
    },
    search(keyword) {
      console.log("关键字搜索", keyword);
      this.keyword = keyword;
      this.page.currentPage = 1;
      // this.getList();
    },
    // 格式化pcwp1.0和pcwp2.0返回的数据并合并
    formattionData(res) {
      let arr = [];
      // pcwp1.0数据
      res.data.forEach((item) => {
        let data = JSON.parse(item.data);
        // console.log(data)
        let obj = {};
        for (let key in data) {
          let lowerCaseKey = key.charAt(0).toLowerCase() + key.slice(1);
          obj[lowerCaseKey] = data[key];
        }
        arr.push({
          data: obj,
          pendingUrl: item.pendingUrl,
        });
      });
      // pcwp2.0数据
      res.data2.forEach((item) => {
        arr.push({
          data: JSON.parse(item.data),
          pendingUrl: item.pendingUrl,
        });
      });
      console.log(arr);
      return arr;
    },
  },
};
</script>

<style scoped lang="scss">
.agenda {
  .content {
  }
}
</style>
